/* eslint-disable */
const validatenull = (val) => {
    if (val instanceof Array) {
      if (val.length === 0) return true
    } else if (val instanceof Object) {
      if (JSON.stringify(val) === '{}') return true
    } else {
      if (val === 'null' || val === null || val === 'undefined' || val === undefined || val === '') return true
      return false
    }
    return false
  }
  
  
  // 深拷贝
  export const deepClone = (o) => {
    // 要判断o是对象还是数组
      if (Array.isArray(o)) {
          // 数组
          var result = []
          for (var i = 0; i < o.length; i++) {
              result.push(deepClone(o[i]))
          }
      } else if (typeof o == 'object') {
          // 对象
          var result = {}
          for (var k in o) {
              result[k] = deepClone(o[k])
          }
      } else {
          // 基本类型值
          var result = o
      }
      return result
  }
  
  // 忽略空值
  export const ignoreNull = (data) => {
      if (!data) {
          return null
      }
      if (data.constructor === Object) {
          let notNullFilter = {}
          for (let key in data) {
              if (!validatenull(data[key])) {
                  notNullFilter[key] = data[key]
              }
          }
          return notNullFilter
      } else {
          return data
      }
  }
  
  export const convertUrlQuery = (data, isPrefix = true) => {
      let prefix = isPrefix ? '?' : ''
      let _result = []
      for (let key in data) {
          let value = data[key]
          // 去掉为空的参数
          if (['', undefined, null].includes(value)) {
              continue
          }
          if (value.constructor === Array) {
              value.forEach(_value => {
                  _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
              })
          } else {
              _result.push(key + '=' + value)
          }
      }
      return _result.length ? prefix + _result.join('&') : ''
  }
  
  export const parseContact = (arr, key) => {
      let newArr = []
      let other = {
          region: '#',
          list: []
      }
      for (let i = 0; i < arr.length; i++) {
          let region = arr[i].region
          if (region.charCodeAt(0) > 64 && region.charCodeAt(0) < 91) {
              let find = newArr.find(item => item.region === region)
              if (find) {
                  find.list.push(arr[i])
              } else {
                  newArr.push({
                      region,
                      list: [arr[i]]
                  })
              }
          } else {
              other.list.push(arr[i])
          }
      }
      newArr.sort((a, b) => a.region.charCodeAt(0) - b.region.charCodeAt(0))
      if (other.list.length > 0) {
          newArr.push(other)
      }
      return newArr
  }
  
  // 字符串隐藏关键信息
  export const hideKeyInfo = (str, frontLen, endLen) => {
      let len = str.length - frontLen - endLen
      let replace = ''
      for (let i = 0; i < len; i++) {
          replace += '*'
      }
      return str.substring(0, frontLen) + replace + str.substring(str.length - endLen)
  }
  
  // 获取a~b之间的随机数
  export const randomNumber = (start, end) => {
      return Math.random() * (end - start + 1) + start
  }